package org.jvnc.rfb.proto;

/**
* <br><br><center><table border="1" width="80%"><hr>
* <p>
* Copyright (C) 2006-2007 by Deon Wu
* <p>
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.0
* of the License, or (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* <a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html">GNU Lesser General Public License</a>
* for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html">
* GNU Lesser General Public License</a> along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* <hr></table></center>
* **/

import gnu.rfb.server.DesCipher;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class AuthenicationFragment extends AbstractDataGram {
	private byte[] challenge = new byte[16];
	private String password = null;
	
	public void readData(DataInput input ) throws IOException{
	      input.readFully(challenge);
	}	
	
	public void setPassword(String password){
		this.password = password;
	  int firstZero = password.indexOf(0);
	  if (firstZero != -1)
		  password = password.substring(0, firstZero);
	
	  byte[] key = {0, 0, 0, 0, 0, 0, 0, 0};
	  System.arraycopy(password.getBytes(), 0, key, 0, password.length());
	
	  DesCipher des = new DesCipher(key);
	
	  des.encrypt(challenge, 0, challenge, 0);
	  des.encrypt(challenge, 8, challenge, 8);
	}
	
	public void writeData(DataOutput output) throws IOException {
		output.write(challenge);
	}
	
}
